package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.client.frontClient.CommentFrontClient;
import com.atguigu.eduservice.entity.EduComment;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.dto.comment.CommentAddInfoDTO;
import com.atguigu.eduservice.mapper.EduCommentMapper;
import com.atguigu.eduservice.service.EduCommentService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.ucenter.entity.vo.UserCommentInfoVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2024-07-09
 */
@Service
public class EduCommentServiceImpl extends ServiceImpl<EduCommentMapper, EduComment> implements EduCommentService {
    private static final Logger log = LoggerFactory.getLogger(EduCommentServiceImpl.class);
    @Autowired
    private CommentFrontClient commentFrontClient;
    //评论分页方法
    @Override
    public Map<String, Object> getCommentPage(Page<EduComment> eduCommentPage) {
        baseMapper.selectPage(eduCommentPage,null);
        List<EduComment> records = eduCommentPage.getRecords();//集合
        long current = eduCommentPage.getCurrent();//获取当前页页码
        long pages = eduCommentPage.getPages();//返回的总页数
        long size = eduCommentPage.getSize();//每页显示的记录数
        long total = eduCommentPage.getTotal();//返回的总记录数据
        boolean hasNext = eduCommentPage.hasNext();//是否有下页
        boolean hasPrevious = eduCommentPage.hasPrevious();//是否有上页
        //用于存储分页信息
        Map<String, Object> commentMap = new HashMap<>();
        commentMap.put("items",records);
        commentMap.put("current",current);
        commentMap.put("pages",pages);
        commentMap.put("size",size);
        commentMap.put("total",total);
        commentMap.put("hasNext",hasNext);
        commentMap.put("hasPrevious",hasPrevious);
        return commentMap;
    }

    @Override
    public int  addCommentInfo(CommentAddInfoDTO eduCommentDTO, String userId) {
        if(StringUtils.isEmpty(userId)){
            log.info("userId:{}",userId);
            throw new GuliException (20001,"请先登录");
        }
        log.info("userId:{}",userId);
        log.info("eduCommentDTO12138:{}",eduCommentDTO);
        EduComment eduComment = new EduComment();
        BeanUtils.copyProperties(eduCommentDTO,eduComment);
        log.info("eduComment12138:{}",eduComment);
        UserCommentInfoVO openIdUserMember = commentFrontClient.getOpenIdMember(userId);
        log.info("userId:{}",userId);

        if(openIdUserMember == null){
            throw new GuliException (20001,"获取用户信息失败");
        }
        eduComment.setMemberId(userId);

        //将获取用户信息保存到评论表中
        eduComment.setMemberId(openIdUserMember.getId());
        eduComment.setNickname(openIdUserMember.getNickname());
        eduComment.setAvatar(openIdUserMember.getAvatar());
        return baseMapper.insert(eduComment);
    }
}
